from __future__ import absolute_import
import logging
from os.path import join
from forest.api import Command, get_tool

class TimeZone(Command):
    ''' Selects the timezone. Timezone is expected to exist in
        /usr/share/zoneinfo.

        An example::

            timezone: UTC
    '''
    __register__ = 'commands.setup.TimeZone'

    #requires_config = ['timezone']

    def run(self, config):
        layout = config.get('timezone', False)

        if not layout:
            return

        localtime_path = config.join_install_root('etc', 'localtime')
        layout_path = join('/usr', 'share', 'zoneinfo', *layout.split('/'))
        get_tool('system.ln')(layout_path, localtime_path, force=True, no_exists=True)

        clock_path = config.join_install_root('etc', 'sysconfig', 'clock')
        get_tool('files.write')(clock_path, 'ZONE="{0}"\n'.format(layout))
